<!-- 总进度安排 蹇泽阳-->
<template>
  <div class="app-container">
    <!-- 切换栏 -->
    <el-card shadow="always" :body-style="{ padding: '5px 10px 5px' }">
      <div class="flex">
        <h4 :class="{ DynamicCondition: state !== true }" @click="state = false" class="mr55">基础资料建档</h4>
        <h4 :class="{ DynamicCondition: state === true }" @click="state = true">进度安排</h4>
      </div>
    </el-card>

    <!-- 基础资料建档 -->
    <div v-show="!state">
      <!-- <Find :OriginalFilter="{ static: true }" :formList="formList3" @findClicked="findClicked4" /> -->
      <div class="flex mt10" style="width: 100%; background-color: white">
        <el-card shadow="never" :body-style="{ padding: '5px 10px 5px' }" style="width: 100%">
          <div class="flex">
            <h1 class="f16">成型组别料定义</h1>
            <!-- <AddOne :tableHeader="tableHeader" :tableData="tableData" @Save="Save" @AddHandle="AddHandle" :unAdd="false" :unSave="false" /> -->
          </div>
          <Table :tableHeader="tableHeader" :tableData="tableData" :isLoading="isLoading" @confirmChange="confirmChange" />
        </el-card>
      </div>
    </div>

    <!-- 进度安排 -->
    <div v-show="state">
      <Find :OriginalFilter="{ static: true }" :formList="formList" @findClicked="findClicked4" />
      <Find :OriginalFilter="{ static: true }" :formList="formList2" @findClicked="findClicked5" :searchImg="false" />
      <div class="flex mt10" style="width: 100%; background-color: white">
        <el-card shadow="never" :body-style="{ padding: '5px 10px 5px' }">
          <h1 class="f16">待排订单资料</h1>
          <Table
            :tableHeader="tableHeader4"
            :tableData="tableData4"
            selection
            :summaryData="summaryData4"
            summary
            :isLoading="isLoading4"
            @selectList="selectTable4List"
            :outTableHeight="100"
          />
          <PageModule
            :PageData="{ total: total4, pageSizeShow: true, buttonS: true }"
            @pageSizeChange="handleSizeChange4"
            @pageChange="handleCurrentChange4"
          />
        </el-card>
        <div class="flex" style="justify-content: center; align-items: center; flex-direction: column"></div>
        <el-card shadow="never" :body-style="{ padding: '5px 10px 5px' }" style="width: 100%">
          <div class="flex" style="align-items: center">
            <span class="f16" style="font-weight: 600">进度安排资料</span>
            <!-- <span class="f12 ml20 mr24" style="color: #0000FF;">双击生产线或序号，可以调整</span> -->
            <el-button type="primary" class="primaryBth ml10" size="default" @click="up">上移</el-button>
            <el-button type="primary" class="primaryBth" size="default" @click="down">下移</el-button>
          </div>
          <Table
            :isLoading="isLoading5"
            :tableHeader="tableHeader5"
            :tableData="tableData5"
            selection
            @selectList="selectTable5List"
            :summaryData="summaryData5"
            summary
            :outTableHeight="100"
          />
          <PageModule :PageData="{ total: total5, buttonS: true }" @pageSizeChange="handleSizeChange5" @pageChange="handleCurrentChange5" />
        </el-card>
      </div>
    </div>

    <el-dialog v-model="dialogVisible2" title="批量改序号" width="25%">
      <el-form-item label="新序号" class="ml20">
        <el-input v-model="newNo" placeholder="" clearable @change="" style="width: 150px; height: 26px"></el-input>
        <el-button type="primary" class="primaryBth ml45" size="default" @click="changeNo">调整序号</el-button>
      </el-form-item>
    </el-dialog>
  </div>
</template>

<script lang="ts">
import { defineComponent } from 'vue'
import LoginWork from '@/utils/LoginWork'
import fileRequest from '@/utils/fileRequest'
import JMessage from '@/utils/JMessage'
export default defineComponent({
  name: 'AggregateSchedulingL'
})
</script>

<script lang="ts" setup>
import { i18n, Jzhcn } from '@/i18n'
import { ElMessage } from 'element-plus'
import { debounce } from 'lodash'
import { watch } from 'vue'
import { selectList, selectSomeList } from '@/plugins/mixin'
import service from '@/utils/request'
import { onMounted, ref } from 'vue'
const dialogVisible = ref(false)
const dialogVisible2 = ref(false)
const deliveryDate = ref('')
const newNo = ref('')
const formList = ref([
  {
    type: 'select',
    selectValue: '',
    selectP: '品牌',
    selectLabel: '',
    selectOptions: []
  },
  {
    type: 'input',
    inputPlaceholder: '请输入指令号',
    inputValue: ''
  },
  {
    type: 'input',
    inputPlaceholder: '请输入货号',
    inputValue: ''
  },
  {
    type: 'date',
    dateLabel: '交期',
    dateValue: ''
  },
  {
    type: 'button',
    checkboxLabel: '更新指令'
  },
  {
    type: 'button',
    checkboxLabel: '指令结束'
  },
  {
    type: 'button',
    checkboxLabel: '指令恢复'
  },
  {
    type: 'button',
    checkboxLabel: '查看结束指令'
  },
  {
    type: 'button',
    checkboxLabel: '查看未结束指令'
  },
  {
    type: 'button',
    checkboxLabel: '安排进度'
  },
  {
    type: 'button',
    checkboxLabel: '置空',
    bgColor: '#1890FF'
  }
])
const formList2 = ref([
  {
    type: 'select',
    selectValue: '成型',
    selectP: '生产组别',
    selectLabel: '',
    selectOptions: []
  },
  {
    type: 'button',
    checkboxLabel: '取消安排'
  },
  {
    type: 'dateSingle',
    dateLabel: '进度表开始日期',
    dateValue: ''
  },
  {
    type: 'button',
    checkboxLabel: '取进度表资料'
  },
  // 小贺说不用
  // {
  //   type: 'button',
  //   checkboxLabel: '批量移线',
  // },
  {
    type: 'button',
    checkboxLabel: '批量改序号'
  },
  {
    type: 'button',
    checkboxLabel: '计算进度(按交期排序)'
  },
  {
    type: 'button',
    checkboxLabel: '计算进度(按调整排序)'
  },
  {
    type: 'button',
    checkboxLabel: '导出进度表'
  },
  {
    type: 'input',
    inputPlaceholder: '请输入指令号',
    inputValue: '',
    inputLabel: ''
  },
  {
    type: 'input',
    inputPlaceholder: '请输入鞋名',
    inputValue: '',
    inputLabel: ''
  },
  {
    type: 'button',
    checkboxLabel: '置空',
    bgColor: '#1890FF'
  }
])
// 表一数据
const tableHeader = ref([
  {
    change: true,
    label: '序号',
    prop: 'seqNo'
  },
  {
    label: '生产组别',
    prop: 'line'
  },
  {
    label: '日产量(双)',
    prop: 'stdQty'
  },
  {
    label: '人数',
    prop: 'persons'
  }
])
// 表一模块
// 表一按钮
const findClicked = (content: string) => {
  if (content === '置空') {
    formList.value.forEach((item: any) => {
      item.inputValue = ''
      item.selectValue = ''
      item.dateValue = ''
    })
  }
}
const tableData = ref([])
const imgList = ref([])
const isLoading = ref(true)
// 总计
interface summaryDataType {
  index: number
  num: number | undefined
}
const summaryData = ref<summaryDataType[]>([
  {
    index: 1,
    num: 0
  },
  {
    index: 2,
    num: 0
  }
])
// 分页器
const currentPage = ref(1)
const pageSize = ref(40)
const total = ref(0)
const handleSizeChange = (val: number) => {
  pageSize.value = val
  getTableList()
}
const handleCurrentChange = (val: number) => {
  currentPage.value = val
  getTableList()
}
// 获取表数据
const getTableList = debounce(async () => {
  isLoading.value = true
  let tableDataList = await service.get<any, any>('/prod/prod/getLine1')
  isLoading.value = false
  tableData.value = tableDataList.map((item: any) => {
    item.change = 'Y'
    return item
  })
  currentRow()
}, 500)
onMounted(() => {
  getTableList()
})
watch(
  () =>
    formList.value.map((item: any) => {
      item.selectValue, item.inputValue, item.dateValue, item.checked
    }),
  () => {
    getTableList()
  }
)
// 点击表给另一张表传数据
const currentTableRow = ref({}) as any
const currentRow = async (row: any = tableData.value[0] ? tableData.value[0] : {}) => {
  currentTableRow.value = row
}
const confirmChange = async (row: any) => {
  const res = (await LoginWork.post('/prod/prod/updatePcLine1', currentTableRow.value)) as any
  if (res.code === 200) {
    JMessage.success('修改成功')
    getTableList()
    return
  } else {
    JMessage.error(res.message)
  }
}

// 表二数据
// 定义点击切换状态
const state = ref(true)

// 进度安排模块
// 1. 待排订单资料
const choose = ref('2')
const imgList4 = ref([])
const findClicked4 = async (content: string) => {
  const selectTable4LotNoLists = selectTable4Lists.value.map((item: any) => {
    return item.lotNo
  })
  if (content === '置空') {
    formList.value.forEach((item: any) => {
      item.inputValue = ''
      item.selectValue = ''
      item.dateValue = ''
    })
  }
  if (content === '查看结束指令') {
    choose.value = '1'
    getTableList4()
  }
  if (content === '查看未结束指令') {
    choose.value = '2'
    getTableList4()
  }
  if (content === '更新指令') {
    const res = await LoginWork.post<any, any>('/prod/prod/updatePcScheduleLot')
    if (res.code === 200) {
      JMessage.success('更新指令成功')
      getTableList4()
    } else {
      JMessage.error(res.message)
    }
    getTableList4()
  }
  if (content === '指令结束') {
    if (selectTable4Lists.value.length === 0) {
      JMessage.warning('请勾选待排订单资料')
      return
    }
    const res = await LoginWork.post<any, any>('/prod/prod/updateRecoverLot', { choose: 1, list: selectTable4LotNoLists })
    if (res.code === 200) {
      JMessage.success('指令结束成功')
      getTableList4()
    } else {
      JMessage.error(res.message)
    }
  }
  if (content === '指令恢复') {
    if (selectTable4Lists.value.length === 0) {
      JMessage.warning('请勾选待排订单资料')
      return
    }
    const res = await LoginWork.post<any, any>('/prod/prod/updateRecoverLot', { choose: 2, list: selectTable4LotNoLists })
    if (res.code === 200) {
      JMessage.success('指令恢复成功')
      getTableList4()
    } else {
      JMessage.error(res.message)
    }
  }
  if (content === '安排进度') {
    if (selectTable4Lists.value.length === 0) {
      JMessage.warning('请勾选待排订单资料')
      return
    }
    if (tableData5.value.length === 0) {
      JMessage.warning('请取进度表资料')
      return
    }
    const res = await LoginWork.post<any, any>('/prod/prod/updateRecoverLot', { choose: 3, list: selectTable4LotNoLists })
    if (res.code === 200) {
      JMessage.success('安排进度成功')
      getTableList4()
      getTableList5()
    } else {
      JMessage.error(res.message)
    }
  }
}
const tableHeader4 = ref([
  {
    label: '指令号',
    prop: 'lotNo',
    width: 148
  },
  {
    label: '品牌',
    prop: 'brand',
    width: 60
  },
  {
    label: '货号',
    prop: 'style',
    width: 80
  },
  {
    label: '颜色',
    prop: 'color',
    width: 80
  },
  {
    label: '客户交期',
    prop: 'deliveryDate',
    width: 80
  },
  {
    label: '订单数量',
    prop: 'orderQty',
    width: 50
  },
  {
    label: '待排数量',
    prop: 'preQty',
    width: 50
  },
  {
    label: '完工数量',
    prop: 'inQty',
    width: 50
  },
  {
    label: '成型欠数',
    prop: 'shortQty',
    width: 50
  },
  {
    type: 'img',
    label: '图片',
    fixed: true,
    prop: 'photoPath',
    srcList: imgList4,
    width: 80
  }
])
const tableData4 = ref([])
const isLoading4 = ref(true)
// 总计
interface summaryDataType {
  index: number
  num: number | undefined
}
const summaryData4 = ref<summaryDataType[]>([
  {
    index: 7,
    num: 0
  }
])
// 分页器
const currentPage4 = ref(1)
const pageSize4 = ref(40)
const total4 = ref(0)
const handleSizeChange4 = (val: number) => {
  pageSize4.value = val
  getTableList4()
}
const handleCurrentChange4 = (val: number) => {
  currentPage4.value = val
  getTableList4()
}
// 获取表数据
const getTableList4 = debounce(async () => {
  isLoading4.value = true
  let tableDataList = await service.post<any, any>('/prod/prod/getPreOrderList', {
    brand: formList.value.find((item: any) => item.selectP && Jzhcn.Find[item.selectP.split('.')[1] as keyof typeof Jzhcn.Find] === '品牌')
      ?.selectValue,
    choose: choose.value,
    style: formList.value.find(
      (item: any) => item.inputPlaceholder && Jzhcn.Find[item.inputPlaceholder.split('.')[1] as keyof typeof Jzhcn.Find] === '请输入货号'
    )?.inputValue,
    deliveryEndDate: formList.value.find(
      (item: any) => item.dateLabel && Jzhcn.Find[item.dateLabel.split('.')[1] as keyof typeof Jzhcn.Find] === '交期'
    )?.dateValue?.[1],
    deliveryStartDate: formList.value.find(
      (item: any) => item.dateLabel && Jzhcn.Find[item.dateLabel.split('.')[1] as keyof typeof Jzhcn.Find] === '交期'
    )?.dateValue?.[0],
    lotNo: formList.value.find(
      (item: any) => item.inputPlaceholder && Jzhcn.Find[item.inputPlaceholder.split('.')[1] as keyof typeof Jzhcn.Find] === '请输入指令'
    )?.inputValue,
    pageNum: currentPage4.value,
    pageSize: pageSize4.value
  })
  isLoading4.value = false
  tableData4.value = tableDataList.map
  total4.value = tableDataList.total
  // currentRow()
  summaryData4.value[0].num = tableDataList.preTotal
  imgList4.value = tableDataList.map.map((item: any) => {
    return item.photoPath
  })
  console.log(imgList4.value)
}, 500)
onMounted(() => {
  getTableList4()
})
watch(
  () =>
    formList.value.map((item: any) => {
      item.selectValue, item.inputValue, item.dateValue, item.checked
    }),
  () => {
    getTableList4()
  }
)

// 2. 进度安排资料
const findClicked5 = async (content: string) => {
  const selectTable5LotNoLists = selectTable5Lists.value.map((item: any) => {
    return item.lotNo
  })
  if (content === '置空') {
    formList2.value.forEach((item: any) => {
      item.inputValue = ''
      item.dateValue = ''
    })
  }
  if (content === '取消安排') {
    if (selectTable5Lists.value.length === 0) {
      JMessage.warning('请勾选进度安排资料')
      return
    }
    const res = await LoginWork.post<any, any>('/prod/prod/updateRecoverLot', { choose: 4, list: selectTable5LotNoLists })
    if (res.code === 200) {
      JMessage.success('取消安排成功')
    } else {
      JMessage.error(res.message)
    }
    getTableList5()
  }
  if (content === '取进度表资料') {
    const res = await LoginWork.post<any, any>('/prod/prod/updateScheduleDetail')
    if (res.code === 200) {
      JMessage.success('取进度表资料成功')
    } else {
      JMessage.error(res.message)
    }
    getTableList5()
  }
  if (content === '计算进度(按交期排序)') {
    const res = await LoginWork.post<any, any>(`/prod/prod/computeProdDate?order=${1}`)
    if (res.code === 200) {
      JMessage.success('计算进度(按交期排序)成功')
    } else {
      JMessage.error(res.message)
    }
    getTableList5()
  }
  if (content === '计算进度(按调整排序)') {
    const res = await LoginWork.post<any, any>(`/prod/prod/computeProdDate?order=${2}`)
    if (res.code === 200) {
      JMessage.success('计算进度(按调整排序)成功')
    } else {
      JMessage.error(res.message)
    }
    getTableList5()
  }
  // if (content === '批量移线') {
  //   dialogVisible.value = true
  // }
  if (content === '批量改序号') {
    dialogVisible2.value = true
  }
  if (content === '导出进度表') {
    const res = (await fileRequest.post('/pcOrder/downloadPcAggSchedule')) as any
    const link = document.createElement('a') // 利用a标签自定义下载文件名
    let blob = new Blob([res.data]) // 创建Blob对象，设置文件类型
    link.style.display = 'none'
    //设置连接
    link.href = window.URL.createObjectURL(blob)
    const FileName = decodeURI(res.headers['content-disposition'].split(`utf-8''`)[1])
    link.download = FileName //自定义文件名字
    document.body.appendChild(link)
    //模拟点击事件
    link.click()
    document.body.removeChild(link)
  }
}
const tableHeader5 = ref([
  {
    label: '生产组别',
    prop: 'line'
  },
  {
    label: '序号',
    prop: 'seqNo'
  },
  {
    width: '150',
    label: '指令号',
    prop: 'lotNo'
  },
  {
    label: '品牌',
    prop: 'brand'
  },
  {
    label: '货号',
    prop: 'style'
  },
  {
    label: '颜色',
    prop: 'color'
  },
  {
    label: '安排数量',
    prop: 'schQty'
  },
  {
    label: '生产天数',
    prop: 'prodDays'
  },
  {
    label: '开始日期',
    prop: 'startDate'
  },
  {
    label: '完成日期',
    prop: 'endDate'
  },
  {
    label: '客户交期',
    prop: 'deliveryDate'
  }
])
const tableData5 = ref([])
const isLoading5 = ref(true)
// 总计
interface summaryDataType {
  index: number
  num: number | undefined
}
const summaryData5 = ref<summaryDataType[]>([
  {
    index: 7,
    num: 0
  },
  {
    index: 8,
    num: 0
  }
])
// 分页器
const currentPage5 = ref(1)
const pageSize5 = ref(40)
const total5 = ref(0)
const handleSizeChange5 = (val: number) => {
  pageSize5.value = val
  getTableList5()
}
const handleCurrentChange5 = (val: number) => {
  currentPage5.value = val
  getTableList5()
}
// 获取表数据
const getTableList5 = debounce(async () => {
  isLoading5.value = true
  let tableDataList = await service.get<any, any>('/prod/prod/getPcScheduleList', {
    params: {
      lotNo: formList2.value.find(
        (item: any) => item.inputPlaceholder && Jzhcn.Find[item.inputPlaceholder.split('.')[1] as keyof typeof Jzhcn.Find] === '请输入指令号'
      )?.inputValue,
      shoeName: formList2.value.find(
        (item: any) => item.inputPlaceholder && Jzhcn.Find[item.inputPlaceholder.split('.')[1] as keyof typeof Jzhcn.Find] === '请输入鞋名'
      )?.inputValue,
      pageNum: currentPage5.value,
      pageSize: pageSize5.value
    }
  })
  isLoading5.value = false
  tableData5.value = tableDataList.list
  total5.value = tableDataList.total
  summaryData5.value[0].num = tableDataList.schTotal
  summaryData5.value[1].num = tableDataList.prodDays
}, 500)
onMounted(() => {
  getTableList5()
})
watch(formList2.value, () => {
  getTableList5()
})
// 上移和下移
const up = async () => {
  if (selectTable5Lists.value.length === 0) {
    JMessage.warning('请勾选进度安排资料')
    return
  }
  const res = await LoginWork.post<any, any>(`/prod/prod/upDown?upDown=${1}`, selectTable5Lists.value)
  if (res.code === 200) {
    JMessage.success('上移成功')
    getTableList5()
  } else {
    JMessage.warning(res.message)
  }
}
const down = async () => {
  if (selectTable5Lists.value.length === 0) {
    JMessage.warning('请勾选进度安排资料')
    return
  }
  const res = await LoginWork.post<any, any>(`/prod/prod/upDown?upDown=${2}`, selectTable5Lists.value)
  if (res.code === 200) {
    JMessage.success('下移成功')
    getTableList5()
  } else {
    JMessage.warning(res.message)
  }
}

// 品牌下拉框获取
const getSelectList = async () => {
  await selectList(formList, () => service.get('/back/order/allOrderCondition'))
}
onMounted(() => {
  getSelectList()
})
const selectTable4Lists = ref([])
const selectTable4List = (selectedList: any) => {
  selectTable4Lists.value = selectedList
}
const selectTable5Lists = ref([])
const selectTable5List = (selectedList: any) => {
  selectTable5Lists.value = selectedList
}

// 点击调整序号触发函数
const changeNo = async () => {
  if (selectTable5Lists.value.length === 0) {
    JMessage.warning('请勾选进度安排资料')
    return
  }
  const res = await LoginWork.post<any, any>('/prod/prod/batchMove', {
    list: selectTable5Lists.value,
    seqNo: newNo.value
  })
  if (res.code === 200) {
    JMessage.success('批量改序号成功')
    getTableList5()
    dialogVisible2.value = false
  } else {
    JMessage.warning(res.message)
  }
}
</script>

<style lang="scss" scoped>
.DynamicCondition {
  color: #f89027;
  border-bottom: 2px solid #f89027;
}
</style>
